#ifndef _____WARD_BOXGROUP_CPP
#define _____WARD_BOXGROUP_CPP



#include "boxGroup.h"
#include "checkbox.h"
#include "driver.h"

boxGroup::boxGroup(int x, int y, int boxStyle, int w, int n, int limit, int r, int c, int hpad, int ypad)
{
int i,j;

xpos = x;
ypos = y;
this->size = n;
this->lim = limit;
this->rows = r;
this->cols = c;
this->hpadding = hpad;
this->ypadding = ypad;
style = boxStyle;
width = w;

if(n > (r*c)) n = r*c;
if(lim < 1) lim = 1;


for(i = 0; i < rows; i++)
{
	for(j = 0; j < cols; j++)
	{
		buttonGroup.push_back(new checkBox(xpos+(i*(16+hpadding)), ypos+(j*(16+ypadding)),width, style));

		n--;
		if(n == 0)
			break;
	}
	if(n == 0)
		break;
}

buttonGroup[0]->setData(1.0);

curr = 0;
selected.push_back(curr);
}

void boxGroup::drawEnabled(int xpos, int ypos)
{
unsigned int i;
for(i = 0; i < buttonGroup.size(); i++)
	buttonGroup[i]->draw();

}

bool boxGroup::clicked(int x, int y)
{
	unsigned int i = 0;

for(; i < buttonGroup.size(); i++)
	if(buttonGroup[i]->clicked(x,y))
	{
		curr = i;
		return true;
	}
		
return false;
}

void boxGroup::select() {if(curr != -1) buttonGroup[curr]->select(); }



void boxGroup::onClick()
{
		if(curr != -1) 
		{  
			buttonGroup[curr]->onClick(); 			
			if(buttonGroup[curr]->getData() == 1)
			{
			selected.push_front(curr);
			std::list<int>::iterator i = selected.begin();
			i++;
			for(; i != selected.end(); i++)
				if(*i == curr)
				{
					selected.erase(i);
					break;
				}
			if(selected.size() > lim)
			{
			std::list<int>::reverse_iterator drop = selected.rbegin();
			buttonGroup[*drop]->select();
			selected.pop_back();
			}
			curr = -1;
			}
			else
			{
			std::list<int>::iterator i = selected.begin();
			for(; i != selected.end(); i++)
				if(*i == curr)
				{
					selected.erase(i);
					break;
				}
			}
		}
}

#endif